package com.xtjc03.serial.codec;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.Charset;

public class ComMsgEncoder extends ProtocolEncoderAdapter {

    private static final Logger logger = LoggerFactory.getLogger(ComMsgEncoder.class);

    @Override
    public void encode(IoSession ioSession, Object o, ProtocolEncoderOutput protocolEncoderOutput) throws Exception {
        logger.info("[Debug]:ComMsgEncoder.encode");
        if (o instanceof String){
            logger.info("[Debug]:String消息");
            String msg = (String) o;
            byte[] msgBytes = msg.getBytes(Charset.forName("gbk"));
            IoBuffer wrap = IoBuffer.wrap(msgBytes);
            protocolEncoderOutput.write(wrap);
            protocolEncoderOutput.flush();
        }
    }
}
